<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
?>
<script type="text/javascript" language="javascript">
    var kodeload=0;
    var generatepiutangStoreGroup= createStoreGroup(false, 'mGgenpiutangsiswa', [
        'nis',
        'nama',
        'kd_kelas',
        'kelas',
        'id_th_ajaran',
        'tahun_ajaran',
        'semester',
        'kd_biaya',
        'biaya',
        {name:'biaya_dikenakan', type:'float'},
        'detail_potongan',
        {name: 'total_potongan', type:'float'},
        {name:'total_biaya', type:'float'},'thbl','fgroup'],
    'fgroup', '<?php echo base_url(); ?>' + 'generatepiutang/get_rows_preview');
    
    var genpotStore=createStoreGroup(false, 'genpotStore_id', 
    ['nis','kd_biaya','biaya',{name: 'potongan', type: 'float'},'keterangan','th_ajaran_mulai','thbl_mulai','thbl_selesai'],'biaya', 
    '<?php echo base_url(); ?>' + 'generatepiutang/get_rows_potongan');
    var genpotPiutangStore=createStoreGroup(false, 'genpotPiutangStore_id', 
    ['nis','kd_biaya','biaya',{name: 'potongan', type: 'float'},'keterangan','id_th_ajaran','thbl'],'biaya', 
    '<?php echo base_url(); ?>' + 'generatepiutang/get_rows_potongan_resgen');
    var combothajaranStoreGen = createStore(false, 'mcombothajaranStoreGen',['id_th_ajaran','tahun_ajaran'],'<?php echo base_url(); ?>' + 'tahunajaran/get_rows_combo');
    var combo_bulangen_Store=createArrayStore2( ['num', 'name'],bulan_data);
    var allsiswaStoreGen = createStore(false, 'mallsiswaStoreGen',['nis','nama','kd_kelas','kelas'],'<?php echo base_url(); ?>' + 'datasiswa/get_rows_all');
    //==========================================================================================
    var twinsiswagenStore = createStore(false, 'mtwinsiswagenStore',['nis','nama','kd_kelas','kelas'],'<?php echo base_url(); ?>' + 'datasiswa/get_rows_twin');
    defineTwinSiswa_windows('Windowfindsiswagen', 'Windowfindsiswagen_id', twinsiswagenStore, function(){			
        var sm = this.getSelectionModel();
        var sel = sm.getSelection();
        //                console.log('doublekil');
        if (sel.length > 0) {	
            Ext.getCmp('gensiswa_nis').setValue(sel[0].get('nis'));
            Ext.getCmp('gensiswa_nama').setValue(sel[0].get('nama'));
//            Ext.getCmp('gensiswa_kelas').setValue(sel[0].get('kelas'));                    
            Ext.getCmp('Windowfindsiswagen_id').close();
//            Ext.getCmp('biayasiswa_id_th_ajaran').focus();
        }
    });
    
    //===================
    Ext.define('Formgen', {
        extend          : 'Ext.form.Panel',
        alias           : 'widget.genForm',
        id              : 'Formgen_id',
        requires        : [
            'Ext.form.Field'
        ],
        defaultType     : 'textfield',
        defaults        : {
            allowBlank: false,
            labelWidth: 120,
            labelAlign: 'right',
            anchor:'90%'
            
        },
        monitorValid: true,
        url: '<?php echo base_url(); ?>' + 'generatepiutang/set_params',
        buttonAlign     : 'center',
        padding         : 5,
        style           : 'background-color: #fff;',
        border          : false,
        initComponent   : function(){
            this.items = [    
                {
                    xtype:'combo',
                    afterLabelTextTpl: required_css,
                    name: 'id_th_ajaran',
                    id: 'gen_id_th_ajaran',
                    fieldLabel: 'Tahun Ajaran',                                    
                    //                    labelWidth: 80,
                    emptyText: 'Tahun Ajaran'
                    ,mode:'local',
                    store: combothajaranStoreGen,
                    valueField: 'id_th_ajaran',
                    displayField: 'tahun_ajaran',
                    typeAhead: true,
                    triggerAction: 'all',                    
                    allowBlank: false,
                    editable: false  ,
                    hiddenName:'id_th_ajaran'
                    ,listeners:{
                        select:function(cmb,rec){
//                            var minyear=rec[0].get('tahun_ajaran');
                            var stryear=rec[0].get('tahun_ajaran');
                            if(stryear){
                               var minyear=stryear.substr(0, 4);
                               var maxyear=stryear.substr(5,9);
                                
                                getComp('gen_tahun').setMinValue( parseInt(minyear));
                                getComp('gen_tahun').setMaxValue( parseInt(maxyear));
                                getComp('gen_tahun').setValue(parseInt(minyear));
                            }
//                            console.log(rec[0].get('tahun_ajaran'));
                        }
                    }
                },{
                    xtype:'fieldcontainer',
                    fieldLabel:'ThBl Periode',
                    //                    labelWidth: 130,
                    layout: 'hbox',                    
                    defaultType: 'textfield',                    
                    //                    margin: '0 0 5 0',
                    defaults: {
                        labelWidth: 80,
                        labelAlign: 'right'
                        ,hideLabel: 'true'
                    },
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    items: [
                        {
                            xtype:'numberfield',
                            name:'tahun',
                            fieldLabel:'Tahun',
                            id: 'gen_tahun',
                            emptyText:'Tahun',
                            allowBlank: false,                                                                        
                            //                            hideTrigger: true,
                            fieldStyle: 'text-align: right;'
                            ,minValue: new Date().getFullYear()-14
                            ,flex:1
                        },
                        {
                            xtype: 'combobox',
                            name: 'bulan',
                            id: 'gen_bulan',
                            displayField: 'name',
                            valueField: 'num',
                            queryMode: 'local',
                            emptyText: 'Bulan',
                            hiddenName:'bulan',
                            //                            margins: '0 6 0 0',
                            store: combo_bulangen_Store,
                            width: 100,
                            allowBlank: false,
                            forceSelection: true
                            ,flex:1
                        }
                    ]
                }     
            ];
            this.buttons = [
                {
                    text: 'Simpan',                    
                    itemId: 'simpan',
                    id:'btn_gen_simpan',
                    iconCls: 'icons-add',
                    formBind: true,
                    scope: this,
                    handler: this.submit
                },
                {
                    text: 'Batal',
                    action: 'cancel',
                    itemId: 'batal',
                    iconCls: 'cancel',
                    handler: function(){
                        this.up('window').close();
                    }
                }
            ];
            this.callParent(arguments);
        }
        ,
        submit: function(){
            var parcmd=Ext.getCmp('btn_gen_simpan').getText();
            var mpotongan=0;
            var thbl=setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());
            var thajaran=Ext.getCmp('gen_id_th_ajaran').getValue();
//            var thblnow=Ext.Date.format(new Date(), 'Ym');
//            if(parseInt(thbl)>parseInt(thblnow)){
//                set_message(2, 'Tahun Bulan Periode Lebih Besar Dari Tahun Bulan Saat Ini !');
//                return;
//            }
//            console.log(thblnow);
            if(parcmd === 'Test'){
                parcmd='preview';
                kodeload=0;
                Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_preview';
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);
                
                var tit=Ext.getCmp('btn_gen_simpan').getText();
                tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());
                        
                Ext.getCmp('gridgenerate').setTitle(tit);
                Ext.getCmp('gridgenerate').store.reload(); 
                Ext.getCmp('Formgen_id').getForm().reset();
                Ext.getCmp('Windowgen_id').close();
            }else if(parcmd === 'Tampilkan'){
                parcmd='preview';
                kodeload=1;
                Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_resgen';
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);

                //                                                            Ext.getCmp('gridgenerate').store.reload(); 
                var tit=Ext.getCmp('btn_gen_simpan').getText();
                tit=tit+' Piutang ThBl ' +setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());

                Ext.getCmp('gridgenerate').setTitle(tit);
                Ext.getCmp('gridgenerate').store.reload(); 
                Ext.getCmp('Formgen_id').getForm().reset();
                Ext.getCmp('Windowgen_id').close();
            
            }else if(parcmd === 'Proses'){
                parcmd='proses';          
                kodeload=1;
                Ext.getCmp('Formgen_id').getForm().submit({
                    url: this.url,
                    scope: this,                               
                    params: {
                        cmd: parcmd,
                        thbl:thbl,
                        id_th_ajaran:thajaran                    
                    },
                    waitMsg: 'Mohon Tunggu Proses...',
                    //                    waitTitle:'Proses Pembuatan Piutang Thbl',
                    success: function(form, action) {   
                        if(action.result.msgcode==0){
                            execute_ajax('generatepiutang/proses_generate', {
                                cmd:'thbl',
                                thbl:thbl,
                                id_th_ajaran:thajaran    
                            }, 'Mohon Tunggu Proses...', function(obj) {
                                var   resp = Ext.decode(obj.responseText);                                                                
                                if(resp.success==true){
                                    //                                                                    Ext.Msg.alert('info',resp.msg);
                                    set_message(0,resp.msg);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_resgen';
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);
                
                                    //                                                            Ext.getCmp('gridgenerate').store.reload(); 
                                    var tit=Ext.getCmp('btn_gen_simpan').getText();
                                    tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());

                                    Ext.getCmp('gridgenerate').setTitle(tit);
                                    Ext.getCmp('gridgenerate').store.reload(); 
                                    Ext.getCmp('Formgen_id').getForm().reset();
                                    Ext.getCmp('Windowgen_id').close();
                                }else{
                                    Ext.Msg.show({
                                        title: 'Error',
                                        msg: resp.msg,
                                        modal: true,
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK,
                                        fn: function(btn){
                                            if (btn == 'ok' && resp.msg == 'Session Expired') {
                                                window.location = '<?= site_url("auth/login") ?>';
                                            }
                                        }
                                    });
                                }
                            });
                        }else{
                            execute_confirm(action.result.msg, 'generatepiutang/proses_generate', 
                            {
                                cmd:'thbl',
                                thbl:thbl,
                                id_th_ajaran:thajaran    
                            }, function(obj) {
                                var   resp = Ext.decode(obj.responseText);                                                                
                                if(resp.success==true){
                                    //                                                                    Ext.Msg.alert('info',resp.msg);
                                    set_message(0,resp.msg);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_resgen';
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);
                
                                    //                                                            Ext.getCmp('gridgenerate').store.reload(); 
                                    var tit=Ext.getCmp('btn_gen_simpan').getText();
                                    tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());

                                    Ext.getCmp('gridgenerate').setTitle(tit);
                                    Ext.getCmp('gridgenerate').store.reload(); 
                                    Ext.getCmp('Formgen_id').getForm().reset();
                                    Ext.getCmp('Windowgen_id').close();
                                }else{
                                    Ext.Msg.show({
                                        title: 'Error',
                                        msg: resp.msg,
                                        modal: true,
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK,
                                        fn: function(btn){
                                            if (btn == 'ok' && resp.msg == 'Session Expired') {
                                                window.location = '<?= site_url("auth/login") ?>';
                                            }
                                        }
                                    });
                                }
                            })
                        }
                        //                        set_message(action.result.msgcode,action.result.msg);   
                        //                        var tit=Ext.getCmp('btn_gen_simpan').getText();
                        //                        tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());
                        //                        Ext.getCmp('gridgenerate').setTitle(tit);
                        //                        Ext.getCmp('gridgenerate').store.reload(); 
                        //                        Ext.getCmp('Formgen_id').getForm().reset();
                        //                        Ext.getCmp('Windowgen_id').close();
                    },
                    failure: function(form, action) {
                        if(action.result.msg=='Session Expired') {
                            session_expired(action.result.msg);
                        }else{
                            set_message(1, action.result.msg);
                        }
                                            
                    
                    }
                });
            }
        },
        showError: function(msg, title){
            title = title || 'Error';
            Ext.Msg.show({
                title: title,
                msg: msg,
                modal: true,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK,
                fn: function(btn){
                    if (btn == 'ok' && msg == 'Session Expired') {
                        window.location = '<?= site_url("auth/login") ?>';
                    }
                }
            });
        }
    }
);
        
    
    Ext.define('Windowgen', {
        extend          : 'Ext.window.Window',
        title           : 'Form Generate Piutang',
        width           : 400,
        height          : 200,
        layout          : 'fit',
        autoShow        : true,
        modal           : true,
        alias           : 'widget.genEdit',
        id              : 'Windowgen_id',
        initComponent   : function(){
            this.items = [
                Ext.widget('genForm')
            ];
            this.callParent(arguments);
        }

    });
//    ==================================================
    Ext.define('Formgensiswa', {
        extend          : 'Ext.form.Panel',
        alias           : 'widget.gensiswaForm',
        id              : 'Formgensiswa_id',
        requires        : [
            'Ext.form.Field'
        ],
        defaultType     : 'textfield',
        defaults        : {
            allowBlank: false,
            labelWidth: 120,
            labelAlign: 'right',
            anchor:'90%'
            
        },
        monitorValid: true,
        url: '<?php echo base_url(); ?>' + 'generatepiutang/set_params_nis',
        buttonAlign     : 'center',
        padding         : 5,
        style           : 'background-color: #fff;',
        border          : false,
        initComponent   : function(){
            this.items = [ 
                {xtype: 'twincombo'
                            ,name: 'nis'
                            ,fieldLabel: 'Nis',
                            id:'gensiswa_nis',
                            store:twinsiswagenStore,
                            menu:'Windowfindsiswagen',
                            enableKeyEvents:true,
                            allowBlank:false
                            ,listeners:{
                                keyup: function(f,e,opt) { 
                                    if (e.getKey()==113){
                                        f.onTrigger1Click();
                                    }   
                                    if (e.getKey()==13){
                                        if(f.getValue().length>0){
                                            var rec=  findNis(f.getValue(), allsiswaStoreGen);
                                            if(rec){

                                                Ext.getCmp('gensiswa_nama').setValue(rec.get('nama'));
//                                                Ext.getCmp('biayasiswa_kelas').setValue(rec.get('kelas')); 
                                                Ext.getCmp('gensiswa_id_th_ajaran').focus();
                                            }else{
                                                set_message(2,'Data Siswa Tidak Terdaftar !!!');
                                                f.setValue('');
                                            }
                                        };
                                    }
                                }
                            }
                        }    
                ,{
                            name: 'nama',
                            id: 'gensiswa_nama',
                            fieldLabel: 'Nama Siswa',
                            maxLength:50,
                            //                                    flex: 2,
//                            labelWidth: 80,
                            //                        width:200,
                            //                                    margins: '0 0 0 5',
                            emptyText: 'Nama Siswa',
                            allowBlank: true
                            ,readOnly:true
                            //                                    ,
                            //                                    tooltip: 'Field tidak boleh kosong',
                            //                                    afterLabelTextTpl: required_css
                        },
                {
                    xtype:'combo',
                    afterLabelTextTpl: required_css,
                    name: 'id_th_ajaran',
                    id: 'gensiswa_id_th_ajaran',
                    fieldLabel: 'Tahun Ajaran',                                    
                    //                    labelWidth: 80,
                    emptyText: 'Tahun Ajaran'
                    ,mode:'local',
                    store: combothajaranStoreGen,
                    valueField: 'id_th_ajaran',
                    displayField: 'tahun_ajaran',
                    typeAhead: true,
                    triggerAction: 'all',                    
                    allowBlank: false,
                    editable: false  ,
                    hiddenName:'id_th_ajaran'
                    ,listeners:{
                        select:function(cmb,rec){
                            var stryear=rec[0].get('tahun_ajaran');
                            if(stryear){
                               var minyear=stryear.substr(0, 4);
                               var maxyear=stryear.substr(5,9);
                                
                                getComp('gensiswa_tahun').setMinValue( parseInt(minyear));
                                getComp('gensiswa_tahun').setMaxValue( parseInt(maxyear));
                                getComp('gensiswa_tahun').setValue(parseInt(minyear));
                            }
                                //                            console.log(rec[0].get('tahun_ajaran'));
                        }
                    }
                },{
                    xtype:'fieldcontainer',
                    fieldLabel:'ThBl Periode',
                    //                    labelWidth: 130,
                    layout: 'hbox',                    
                    defaultType: 'textfield',                    
                    //                    margin: '0 0 5 0',
                    defaults: {
                        labelWidth: 80,
                        labelAlign: 'right'
                        ,hideLabel: 'true'
                    },
                    tooltip: 'Field tidak boleh kosong',
                    afterLabelTextTpl: required_css,
                    items: [
                        {
                            xtype:'numberfield',
                            name:'tahun',
                            fieldLabel:'Tahun',
                            id: 'gensiswa_tahun',
                            emptyText:'Tahun',
                            allowBlank: false,                                                                        
                            //                            hideTrigger: true,
                            fieldStyle: 'text-align: right;'
                            ,minValue: new Date().getFullYear()-14
                            ,flex:1
                        },
                        {
                            xtype: 'combobox',
                            name: 'bulan',
                            id: 'gensiswa_bulan',
                            displayField: 'name',
                            valueField: 'num',
                            queryMode: 'local',
                            emptyText: 'Bulan',
                            hiddenName:'bulan',
                            //                            margins: '0 6 0 0',
                            store: combo_bulangen_Store,
                            width: 100,
                            allowBlank: false,
                            forceSelection: true
                            ,flex:1
                        }
                    ]
                }     
            ];
            this.buttons = [
                {
                    text: 'Simpan',                    
                    itemId: 'simpan',
                    id:'btn_gensiswa_simpan',
                    iconCls: 'icons-add',
                    formBind: true,
                    scope: this,
                    handler: this.submit
                },
                {
                    text: 'Batal',
                    action: 'cancel',
                    itemId: 'batal',
                    iconCls: 'cancel',
                    handler: function(){
                        this.up('window').close();
                    }
                }
            ];
            this.callParent(arguments);
        }
        ,
        submit: function(){
            var parcmd=Ext.getCmp('btn_gensiswa_simpan').getText();
            var mpotongan=0;
            var thbl=setThbl(Ext.getCmp('gensiswa_tahun').getValue(), Ext.getCmp('gensiswa_bulan').getValue());
            var thajaran=Ext.getCmp('gensiswa_id_th_ajaran').getValue();
            var nis=Ext.getCmp('gensiswa_nis').getValue();
            var thblnow=Ext.Date.format(new Date(), 'Ym');
//            if(parseInt(thbl)>parseInt(thblnow)){
//                set_message(2, 'Tahun Bulan Periode Lebih Besar Dari Tahun Bulan Saat Ini !');
//                return;
//            }
            if(parcmd === 'Test'){
                parcmd='preview';
                kodeload=2;
                Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_preview_nis';
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);
                Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("nis",nis);
                
                var tit=Ext.getCmp('btn_gensiswa_simpan').getText();
                tit=tit+' Pembuatan Piutang Siswa ' +setThbl(Ext.getCmp('gensiswa_tahun').getValue(), Ext.getCmp('gensiswa_bulan').getValue());
                        
                Ext.getCmp('gridgenerate').setTitle(tit);
                Ext.getCmp('gridgenerate').store.reload(); 
                Ext.getCmp('Formgensiswa_id').getForm().reset();
                Ext.getCmp('Windowgen_id').close();
            }else if(parcmd === 'Proses'){
                parcmd='proses';          
                kodeload=3;
                Ext.getCmp('Formgensiswa_id').getForm().submit({
                    url: this.url,
                    scope: this,                               
                    params: {
                        cmd: parcmd,
                        thbl:thbl,
                        id_th_ajaran:thajaran                    
                    },
                    waitMsg: 'Mohon Tunggu Proses...',
                    //                    waitTitle:'Proses Pembuatan Piutang Thbl',
                    success: function(form, action) {   
                        if(action.result.msgcode==0){
                            execute_ajax('generatepiutang/proses_generate', 
                            {
                                cmd:'nis',
                                thbl:thbl,
                                id_th_ajaran:thajaran,
                                nis:nis
                                
                            }, 'Mohon Tunggu Proses...', 
                            function(obj) {
                                var   resp = Ext.decode(obj.responseText);                                                                
                                if(resp.success==true){
                                    //                                                                    Ext.Msg.alert('info',resp.msg);
                                    set_message(0,resp.msg);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_resgen_nis';
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("nis",nis);
                
                                    //                                                            Ext.getCmp('gridgenerate').store.reload(); 
                                    var tit=Ext.getCmp('btn_gensiswa_simpan').getText();
                                    tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gensiswa_tahun').getValue(), Ext.getCmp('gensiswa_bulan').getValue());

                                    Ext.getCmp('gridgenerate').setTitle(tit);
                                    Ext.getCmp('gridgenerate').store.reload(); 
                                    Ext.getCmp('Formgensiswa_id').getForm().reset();
                                    Ext.getCmp('Windowgen_id').close();
                                }else{
                                    Ext.Msg.show({
                                        title: 'Error',
                                        msg: resp.msg,
                                        modal: true,
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK,
                                        fn: function(btn){
                                            if (btn == 'ok' && resp.msg == 'Session Expired') {
                                                window.location = '<?= site_url("auth/login") ?>';
                                            }
                                        }
                                    });
                                }
                            });
                        }else{
                            execute_confirm(action.result.msg, 'generatepiutang/proses_generate', 
                            {
                                cmd:'nis',
                                thbl:thbl,
                                id_th_ajaran:thajaran,
                                nis:nis
                                
                            }, function(obj) {
                                var   resp = Ext.decode(obj.responseText);                                                                
                                if(resp.success==true){
                                    //                                                                    Ext.Msg.alert('info',resp.msg);
                                    set_message(0,resp.msg);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().api.read='<?php echo base_url(); ?>' + 'generatepiutang/get_rows_resgen_nis';
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("thbl",thbl);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("id_th_ajaran",thajaran);
                                    Ext.getCmp('gridgenerate').getStore().getProxy().setExtraParam("nis",nis);
                
                                    //                                                            Ext.getCmp('gridgenerate').store.reload(); 
                                    var tit=Ext.getCmp('btn_gensiswa_simpan').getText();
                                    tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gensiswa_tahun').getValue(), Ext.getCmp('gensiswa_bulan').getValue());

                                    Ext.getCmp('gridgenerate').setTitle(tit);
                                    Ext.getCmp('gridgenerate').store.reload(); 
                                    Ext.getCmp('Formgensiswa_id').getForm().reset();
                                    Ext.getCmp('Windowgen_id').close();
                                }else{
                                    Ext.Msg.show({
                                        title: 'Error',
                                        msg: resp.msg,
                                        modal: true,
                                        icon: Ext.Msg.ERROR,
                                        buttons: Ext.Msg.OK,
                                        fn: function(btn){
                                            if (btn == 'ok' && resp.msg == 'Session Expired') {
                                                window.location = '<?= site_url("auth/login") ?>';
                                            }
                                        }
                                    });
                                }
                            })
                        }
                        //                        set_message(action.result.msgcode,action.result.msg);   
                        //                        var tit=Ext.getCmp('btn_gen_simpan').getText();
                        //                        tit=tit+' Pembuatan Piutang ThBl ' +setThbl(Ext.getCmp('gen_tahun').getValue(), Ext.getCmp('gen_bulan').getValue());
                        //                        Ext.getCmp('gridgenerate').setTitle(tit);
                        //                        Ext.getCmp('gridgenerate').store.reload(); 
                        //                        Ext.getCmp('Formgen_id').getForm().reset();
                        //                        Ext.getCmp('Windowgen_id').close();
                    },
                    failure: function(form, action) {
                        if(action.result.msg=='Session Expired') {
                            session_expired(action.result.msg);
                        }else{
                            set_message(1, action.result.msg);
                        }
                                            
                    
                    }
                });
            }
        },
        showError: function(msg, title){
            title = title || 'Error';
            Ext.Msg.show({
                title: title,
                msg: msg,
                modal: true,
                icon: Ext.Msg.ERROR,
                buttons: Ext.Msg.OK,
                fn: function(btn){
                    if (btn == 'ok' && msg == 'Session Expired') {
                        window.location = '<?= site_url("auth/login") ?>';
                    }
                }
            });
        }
    }
);
//======================================================
    function getColums_pot(opt){
        if(opt==0){
            return [
                {
                    header: "Tahun Ajaran Mulai",
                    dataIndex: 'th_ajaran_mulai',
                    sortable: true,
                    width: 120     
                    ,align:'center'
                    //                                                ,hidden:true
                },{
                    header: "Keterangan",
                    dataIndex: 'keterangan',
                    sortable: true,
                    width: 100     
                    ,align:'left'
                    //                                                ,hidden:true
                },{
                    xtype:'numbercolumn',
                    header: "Potongan",
                    dataIndex: 'potongan',
                    sortable: true,
                    width: 100     
                    ,align:'right'
                    ,format:'0,0'
                    ,summaryType: 'sum'
                    ,summaryRenderer: function(value, summaryData, dataIndex) {
                        Ext.util.Format.thousandSeparator = ',';
                        Ext.util.Format.decimalSeparator = '.';
                        //                            metaData.tdCls='x-bls-header';
                        return Ext.util.Format.number(value,'0,000/i');
                    }
                },{
                    header: "ThBl Mulai",
                    dataIndex: 'thbl_mulai',
                    sortable: true,
                    width: 80   
                    ,align:'center'
                },{
                    header: "ThBl Selesai",
                    dataIndex: 'thbl_selesai',
                    sortable: true,
                    width: 80   
                    ,align:'center'
                }
            ]
        }
        if(opt==1){
            return [
                {
                    header: "Keterangan",
                    dataIndex: 'keterangan',
                    sortable: true,
                    width: 100     
                    ,align:'left'
                    //                                                ,hidden:true
                },{
                    xtype:'numbercolumn',
                    header: "Potongan",
                    dataIndex: 'potongan',
                    sortable: true,
                    width: 100     
                    ,align:'right'
                    ,format:'0,0'
                    ,summaryType: 'sum'
                    ,summaryRenderer: function(value, summaryData, dataIndex) {
                        Ext.util.Format.thousandSeparator = ',';
                        Ext.util.Format.decimalSeparator = '.';
                        //                            metaData.tdCls='x-bls-header';
                        return Ext.util.Format.number(value,'0,000/i');
                    }
                }
            ]
        }
    }
    Ext.define('Windowgenpot', {
        extend          : 'Ext.window.Window',
        title           : 'Form Detail Potongan',
        width           : 500,
        height          : 350,
        layout          : 'fit',
        autoShow        : true,
        modal           : true,
        alias           : 'widget.genpotEdit',
        id              : 'Windowgenpot_id',
        initComponent   : function(){
            this.items = [
                {xtype:'grid',
                    id:'gridgenpot',
                    stateful:true,
                    stateId:'stateGridgenpot',
                    store: genpotStore,//Ext.data.StoreManager.lookup('mMasterAccountStore'),
                    stripeRows: true,
                    loadMask: true   
                    ,
                    features: [{
                            id: 'group',
                            ftype: 'groupingsummary',
                            //            groupHeaderTpl: '{name}',
                            hideGroupedHeader: true,
                            enableGroupingMenu: false,
                            toggleSummaryRow:true
                        }]
                    ,columns:[
                        {
                            header: "Tahun Ajaran Mulai",
                            dataIndex: 'th_ajaran_mulai',
                            sortable: true,
                            width: 120     
                            ,align:'center'
                            //                                                ,hidden:true
                        },{
                            header: "Keterangan",
                            dataIndex: 'keterangan',
                            sortable: true,
                            width: 100     
                            ,align:'left'
                            //                                                ,hidden:true
                        },{
                            xtype:'numbercolumn',
                            header: "Potongan",
                            dataIndex: 'potongan',
                            sortable: true,
                            width: 100     
                            ,align:'right'
                            ,format:'0,0'
                            ,summaryType: 'sum'
                            ,summaryRenderer: function(value, summaryData, dataIndex) {
                                Ext.util.Format.thousandSeparator = ',';
                                Ext.util.Format.decimalSeparator = '.';
                                //                            metaData.tdCls='x-bls-header';
                                return Ext.util.Format.number(value,'0,000/i');
                            }
                        },{
                            header: "ThBl Mulai",
                            dataIndex: 'thbl_mulai',
                            sortable: true,
                            width: 80   
                            ,align:'center'
                        },{
                            header: "ThBl Selesai",
                            dataIndex: 'thbl_selesai',
                            sortable: true,
                            width: 80   
                            ,align:'center'
                        }
                    ]
                }
            ];
            this.callParent(arguments);
        }

    });
    
    defineTabForm(
    {
        name:'MyTabPembuatanPiutangSiswa',
        xtype:'TabPembuatanPiutangSiswa',
        alias:'widget.TabPembuatanPiutangSiswa',
        title:'Pembuatan Piutang Siswa',
        id:'tab11'
    },
    {
        id:'panelpiutang',
        items:[
            {
                xtype:'grid',
                id:'gridgenerate',
                title:'',
                stateful:true,
                stateId:'stateGridgenerate',
                store: generatepiutangStoreGroup,//Ext.data.StoreManager.lookup('mMasterAccountStore'),
                stripeRows: true,
                loadMask: true        
                ,columnLines:true
                ,columns:[
                    {
                        header: "NIS",
                        dataIndex: 'nis',
                        sortable: true,
                        width: 100,
                        //                                groupable:      true,groupHeaderTpl: '{name}',
                        align:'center'
                        ,hidden:true
                                
                    }
                    ,{
                        header: "Nama Siswa",
                        dataIndex: 'nama',
                        sortable: true,
                        width: 120      
                        ,hidden:true
                    }
                    ,{
                        header: "kd_kelas",
                        dataIndex: 'kd_kelas',
                        sortable: true,
                        width: 50     
                        ,align:'center'
                        ,hidden:true
                    }
                    ,{
                        header: "Kelas",
                        dataIndex: 'kelas',
                        sortable: true,
                        width: 80     
                        ,align:'center'
                        ,hidden:true
                    } ,{
                        header: "Id Tahun Ajaran",
                        dataIndex: 'id_th_ajaran',
                        sortable: true,
                        width: 80     
                        ,align:'center'
                        ,hidden:true
                    },{
                        header: "Tahun Ajaran",
                        dataIndex: 'tahun_ajaran',
                        sortable: true,
                        width: 100     
                        ,align:'center'
                        ,hidden:true
                    },{
                        header: "Semester",
                        dataIndex: 'semester',
                        sortable: true,
                        width: 100     
                        ,align:'center'
                        ,hidden:true
                    },{
                        header: "Kode Biaya",
                        dataIndex: 'kd_biaya',
                        sortable: true,
                        width: 80     
                        ,align:'center'
                        //                        ,columnLines:true
                        //                                ,hidden:true
                    }
                    ,{
                        header: "Biaya",
                        dataIndex: 'biaya',
                        sortable: true,
                        width: 120   
                        ,summaryType: function(records){
                            return 'Total';
                        }
                        //                        ,columnLines:true
                        //                        
                        //                                ,align:'center'
                    },{
                        xtype:'numbercolumn',
                        header: "Biaya Dikenakan",
                        dataIndex: 'biaya_dikenakan',
                        sortable: true,
                        width: 100     
                        ,align:'right'
                        ,format:'0,0'
                        ,summaryType: 'sum'
                        //                        ,columnLines:true
                    },{
                        header: "Detail Potongan",
                        dataIndex: 'detail_potongan',
                        sortable: true,
                        width: 120                             
                    },{
                        xtype:'numbercolumn',
                        header: "Total Potongan",
                        dataIndex: 'total_potongan',
                        sortable: true,
                        width: 100     
                        ,align:'right'
                        ,format:'0,0'
                        ,summaryType: 'sum'
                        //                        ,columnLines:true
                    },{
                        xtype:'numbercolumn',
                        header: "Total Biaya",
                        dataIndex: 'total_biaya',
                        sortable: true,
                        width: 100     
                        ,align:'right'
                        ,format:'0,0'
                        ,summaryType: 'sum'
                        //                        ,columnLines:true
                    },{
                        xtype: 'actioncolumn',
                        width: 80,
                        sortable: false,
                        menuDisabled: true,
                        header: "View Potongan",
                        align:'center',
                        items: [{
                                iconCls: 'icon-preview',
                                tooltip: 'View Potongan',
                                scope: this,
                                handler: function(grid, rowIndex, colIndex){
                                    var selection= grid.getStore().getAt(rowIndex);
                                    //                                    kodeload=0;
                                    if (selection) {
                                        var thbl=selection.get('thbl');
                                        var nis =selection.get('nis');
                                        var kd_biaya=selection.get('kd_biaya');
                                        var wingenpot=Ext.create('Windowgenpot');
                                        //                                        var gridpot=Ext.getCmp('gridgenpot');
                                        if(kodeload==0 || kodeload==2){
                                            Ext.getCmp('gridgenpot').reconfigure(genpotStore,getColums_pot(0));
                                        }
                                        if(kodeload==1 || kodeload==3){
                                            Ext.getCmp('gridgenpot').reconfigure(genpotPiutangStore,getColums_pot(1));
                                        }
                                        
                                        var store=Ext.getCmp('gridgenpot').getStore();
                                        store.load({params:{
                                                thbl:thbl,
                                                nis:nis,
                                                kd_biaya:kd_biaya
                                            }});
                                        
                                        //                                        wingenpot.setTitle('Add Form');                                                                        
                                        wingenpot.show();
                                    
                                        //                                            vGrid.getStore().remove(selection);                                            
                                    }
                                        
                                }
                            }]
                    }
                ]
                ,tbar:{
                    xtype: 'toolbar',
                    padding:'2 0 2 5',
                    items: [
                        {
                            text: 'Pembuatan Piutang PerThBl',
                            iconCls: 'icon-imgadd',
                            menu:[
                                {
                                    text: 'Tes Pembuatan Piutang',
                                    iconCls: 'icon-tablerefresh'
                                    ,handler:function(){
                                        var wingen=Ext.create('Windowgen');
                                        wingen.setTitle('Add Form');                                
                                        Ext.getCmp('btn_gen_simpan').setText('Test');
                                        Ext.getCmp('btn_gen_simpan').setIconCls('icon-proses');
                                        wingen.show();
                                    }
                                },{
                                    text: 'Proses Pembuatan Piutang',
                                    iconCls: 'icon-proses',
                                    handler:function(){
                                        var wingen=Ext.create('Windowgen');
                                        wingen.setTitle('Add Form');                                
                                        Ext.getCmp('btn_gen_simpan').setText('Proses');
                                        Ext.getCmp('btn_gen_simpan').setIconCls('icon-proses');
                                        wingen.show();
                                    } 
                                }
                            ]
                        },'-',
                        {
                            text: 'Pembuatan Piutang PerSiswa',
                            iconCls: 'icon-useradd',
                            menu:[
                             {
                                    text: 'Tes Pembuatan Piutang PerSiswa',
                                    iconCls: 'icon-tablerefresh'
                                    ,handler:function(){
                                        allsiswaStoreGen.load();
                                        var wingen=Ext.create('Windowgen');
                                        wingen.removeAll();
                                        wingen.add(Ext.widget('gensiswaForm'));
                                        wingen.setTitle('Add Form');                                
                                        Ext.getCmp('btn_gensiswa_simpan').setText('Test');
                                        Ext.getCmp('btn_gensiswa_simpan').setIconCls('icon-proses');
                                        wingen.show();
                                    }
                                },
                                {                            
                                    text: 'Proses Pembuatan Piutang PerSiswa',
                                    iconCls: 'icon-proses',
                                    handler: function()
                                    {   
                                        allsiswaStoreGen.load();
                                        var wingen=Ext.create('Windowgen');
                                        wingen.removeAll();
                                        wingen.add(Ext.widget('gensiswaForm'));
                                        wingen.setTitle('Add Form');                                
                                        Ext.getCmp('btn_gensiswa_simpan').setText('Proses');
                                        Ext.getCmp('btn_gensiswa_simpan').setIconCls('icon-proses');
                                        wingen.show();
                                    } 
                                }
                            ]
                        }                  
                        
                        ,'-'
                        ,'->','-'
                        ,{
                            xtype: 'button',
                            text: 'Lihat Piutang',
                            iconCls: 'icon-preview',
                            onClick: function()
                            {   
                                var wingen=Ext.create('Windowgen');
                                wingen.setTitle('Add Form');                                
                                Ext.getCmp('btn_gen_simpan').setText('Tampilkan');
                                Ext.getCmp('btn_gen_simpan').setIconCls('icon-proses');
                                wingen.show();
                            } 
                        },'-'
                        ,{
                            width: 300,
                            //                                fieldLabel: 'Search',
                            //                                labelWidth: 50,
                            xtype: 'searchfield',
                            store: generatepiutangStoreGroup,
                            emptyText: 'Quick Search...'
                        }
                    ]
                }
                ,bbar:{                        
                    xtype: 'pagingtoolbar',
                    store: generatepiutangStoreGroup,
                    pageSize: ENDPAGE,
                    displayInfo: true
                    //                    plugins: Ext.create('Ext.ux.SlidingPager', {})
                }
                        
                ,features:[ {
                        ftype: 'groupingsummary',
                        //                                groupHeaderTpl: '{columnName}: {name} ({rows.length} Item{[values.rows.length > 1 ? "s" : ""]})',
                        groupHeaderTpl: '{name}',
                        hideGroupedHeader: true,
                        startCollapsed: false,
                        id: 'gen_grid_Grouping'
                    }]
                
            }
        ]
    },
    function(){
//            var thblnow=Ext.Date.format(new Date(), 'Ym');
//            console.log(thblnow);
    }        
);
        
        
</script>